
package localAI.graphics;

import java.awt.BorderLayout;
import java.awt.Frame;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.HashMap;
import java.util.Vector;
import localAI.env.def.*;

/**
 *
 * @author Isaac
 *
 *	This class is the main class of the server
 */

public class Ui {
	private static int Xsize;
	private static int Ysize;
	private static Frame frame=null;
	private static Boom bmain;
	private static Vector agentsVect;
    private static HashMap<String,SingleAgent> myAgents;

	/**
	 * This procedure parses the message which was accepted from the client
	 *
	 * @param in - a message to be parsed by the server
	 * @return - a message to be sent as a feedback to the client.
	 */


    public static boolean makeBoard(int Xsize_, int Ysize_) {
        Xsize = Xsize_;
        Ysize = Ysize_;
        if(frame!=null)
           frame.dispose();
        frame = new Frame("Board");
	    //create a frame for the crawler window applet
 	    bmain = new Boom();
	    frame.add(bmain,BorderLayout.CENTER);
	    frame.setSize(Xsize*32,Ysize*32);
	    frame.setLocation(20,20);
            frame.setAlwaysOnTop(true);
	    bmain.init(Xsize,Ysize);
	    bmain.start();
	    frame.setVisible(true);
	    frame.addWindowListener(new WindowAdapter() {
            @Override
	    			public void windowClosing(WindowEvent e) {
	    				  frame.dispose();
                          frame=null;
	    			     }
	    			});
        myAgents = new HashMap<String,SingleAgent>();
        agentsVect=new Vector();
        return true;
    }

    public static boolean addBoardElement(BoardElement element, int pos) {
        System.out.printf("adding %s at %d\n",element.toString(),pos);
        switch (element) {
           
           case wall : { bmain.setWall(pos-1); break; }
           case flag : { bmain.setFlag(pos-1); break; }
           //case ice : { Ui.bmain.setWall(pos);break; }
           default: return false;
        }
        return true;
    }

    public static boolean addAgent(String name,String color,Orientation orientation,
			int pos) {
            SingleAgent agent1 = new SingleAgent(name,color,orientation,pos-1,Xsize,Ysize);
	        bmain.setAgent(agent1);
	        agentsVect.addElement(agent1);
            myAgents.put(name, agent1);
        return true;
    }

    public static boolean moveAgent(String name,Direction dir) {
        SingleAgent tmp = myAgents.get(name);
        if (tmp == null) {
            System.out.println("Can't find agent");
            return false;
        }
        switch (dir) {
           case fwd : {
               int newCell = tmp.getNewCell();
	       bmain.deleteContent(tmp.getLocation());
	       tmp.setLocation(newCell);
	       bmain.setAgent(tmp);
               break;
           }
            case left : case right : {
	         	Orientation newOrientation = tmp.getNewOrientation(dir);
	         	bmain.deleteContent(tmp.getLocation());
	         	tmp.setOrientation(newOrientation);
	         	bmain.setAgent(tmp);
                        break;
            }
            default: return false;

        }
        return true;

    }

}
